﻿using System;
using System.Collections.Generic;
using Gaea.SqlServer;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

namespace Gaea.SqlServer.ConsoleTest
{
    class Program
    {
        class LandmarkArray
        {
            [JsonIgnore]
            public string Name { get; set; }

            public int ExitID { get; set; }

            public IEnumerable<Landmark> Landmarks { get; set; }
        }

        class StationDetailArray
        {
            public string Description { get; set; }

            public string StationWikiUrl { get; set; }

            public int Id { get; set; }

            public string Flag { get; set; }

            public string Name { get; set; }

            public string EnglishName { get; set; }

            public double X { get; set; }

            public double Y { get; set; }

            public int CoordType { get; set; }

            public List<object> LineList { get; set; }

            public List<object> ExitList { get; set; }

            public string ImageUrl { get; set; }

            public string StationUrl { get; set; }

            public string StationMerchantUrl { get; set; }

            public string FacilityUrl { get; set; }

            public string LineNOs { get; set; }
        }

        static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();
            GaeaSqlServerPower.Register("Server=rm-bp1xtsqpr49r7fh4ho.sqlserver.rds.aliyuncs.com,3433;Database=subway;User ID=dbtrans;Password=Zhang123;Trusted_Connection=False", "sqlserver");

            var stationCacheAdpater = new StationCacheAdpater();


            var stations = new StationAdpater().FindByLineNos("3");

            foreach (var item in stations)
            {

                var landmarkArrayList = new List<LandmarkArray>();

                var outboundAdpater = new OutboundAdpater();
                var outbounds = outboundAdpater.FindByStationID(item.Id + 6);
                var poi = new POIAdpater().FindByStationId(item.Id);
                foreach (var outbound in outbounds)
                {
                    var landmarks = new LandmarkAdpater().FindByOutboundID(outbound.Id);

                    landmarkArrayList.Add(new LandmarkArray()
                    {
                        Name = outbound.Name,
                        ExitID = outbound.Id,
                        Landmarks = landmarks,
                    });
                }

                var stationDetailArray = new StationDetailArray()
                {
                    CoordType = 5,
                    Description = item.Description,
                    EnglishName = item.EnglishName,
                    FacilityUrl = string.Empty,
                    Flag = string.Empty,
                    Id = item.Id,
                    ImageUrl = string.Empty,
                    Name = item.Name,
                    LineNOs = "3",
                    StationMerchantUrl = string.Empty,
                    StationWikiUrl = string.Empty,
                    StationUrl = string.Empty,
                    ExitList = new List<object>(),
                    LineList = new List<object>(),
                    X = poi.Longitude,
                    Y = poi.Latitude,
                };
                foreach (var landmarkArray in landmarkArrayList)
                {

                    stationDetailArray.ExitList.Add(new
                    {
                        ID = landmarkArray.ExitID,
                        StationID = item.Id,
                        X = poi.Longitude,
                        Y = poi.Latitude,
                        CoordType = 0,
                        LandmarkList = landmarkArray.Landmarks,
                        BusStationList = new string[0],
                        BikePointList = new string[0],
                        FacilityList = new string[0],
                        BusLineList = new string[0],
                        landmarkArray.Name
                    });
                }

                var lines = new Test11Adpater().FindByStation(item.Name).ToArray();
                stationDetailArray.LineList.Add(new
                {
                    ID = "3",
                    TypeName = "第3路线",
                    HandTime = lines[0].first,
                    EndTime = lines[0].end,
                    Way = "开往大通桥",
                    StartStation = "高塘桥",
                    EndStation = "大通桥"
                });
                stationDetailArray.LineList.Add(new
                {
                    ID = "3",
                    TypeName = "第3路线",
                    HandTime = lines[1].first,
                    EndTime = lines[1].end,
                    Way = "开往高塘桥",
                    StartStation = "大通桥",
                    EndStation = "高塘桥"
                });


                var stationCache = new StationCache()
                {
                    StationID = item.Id,
                    LandmarkArray = JsonConvert.SerializeObject(landmarkArrayList),
                    StationDetailArray = JsonConvert.SerializeObject(stationDetailArray),
                };
                stationCacheAdpater.Insert(stationCache);
                Console.WriteLine($"station ：{item.Name} success");
            }



            Console.ReadKey();
        }
    }
}
